package com.rentalcar.wechat.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.rentalcar.util.PageHelper;
import com.rentalcar.wechat.vo.CancelOrderParams;
import com.rentalcar.wechat.vo.OrderItem;
import com.rentalcar.wechat.vo.QueryOrderCriteria;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rentalcar.wechat.mapper.UmsRentalOrderMapper;
import com.rentalcar.wechat.domain.UmsRentalOrder;
import com.rentalcar.wechat.service.IUmsRentalOrderService;
import org.springframework.util.CollectionUtils;

/**
 * 租车信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-20
 */
@Service
@Slf4j
public class UmsRentalOrderServiceImpl implements IUmsRentalOrderService 
{
    @Autowired
    private UmsRentalOrderMapper umsRentalOrderMapper;

    /**
     * 查询租车信息
     * 
     * @param orderId 租车信息主键
     * @return 租车信息
     */
    @Override
    public UmsRentalOrder selectUmsRentalOrderByOrderId(Long orderId)
    {
        return umsRentalOrderMapper.selectUmsRentalOrderByOrderId(orderId);
    }

    /**
     * 查询租车信息列表
     * 
     * @param umsRentalOrder 租车信息
     * @return 租车信息
     */
    @Override
    public List<UmsRentalOrder> selectUmsRentalOrderList(UmsRentalOrder umsRentalOrder)
    {
        return umsRentalOrderMapper.selectUmsRentalOrderList(umsRentalOrder);
    }

    /**
     * 新增租车信息
     * 
     * @param umsRentalOrder 租车信息
     * @return 结果
     */
    @Override
    public int insertUmsRentalOrder(UmsRentalOrder umsRentalOrder)
    {
        return umsRentalOrderMapper.insertUmsRentalOrder(umsRentalOrder);
    }

    /**
     * 修改租车信息
     * 
     * @param umsRentalOrder 租车信息
     * @return 结果
     */
    @Override
    public int updateUmsRentalOrder(UmsRentalOrder umsRentalOrder)
    {
        return umsRentalOrderMapper.updateUmsRentalOrder(umsRentalOrder);
    }

    /**
     * 批量删除租车信息
     * 
     * @param orderIds 需要删除的租车信息主键
     * @return 结果
     */
    @Override
    public int deleteUmsRentalOrderByOrderIds(Long[] orderIds)
    {
        return umsRentalOrderMapper.deleteUmsRentalOrderByOrderIds(orderIds);
    }

    /**
     * 删除租车信息信息
     * 
     * @param orderId 租车信息主键
     * @return 结果
     */
    @Override
    public int deleteUmsRentalOrderByOrderId(Long orderId)
    {
        return umsRentalOrderMapper.deleteUmsRentalOrderByOrderId(orderId);
    }

    @Override
    public List<UmsRentalOrder> queryOrderByOrderCode(String orderCode, long customerId, OrderItem... orderItems) {
        log.debug("queryOrderById and orderCode:{} \r\n customerId:{}", orderCode, customerId);
        Map<String, Object> params = new HashMap<>();
        params.put("orderCode", orderCode);
        params.put("customerId", customerId);
        List<UmsRentalOrder> orders = umsRentalOrderMapper.queryOrderByOrderCode(params);
        if (CollectionUtils.isEmpty(orders)) {
            log.error("queryOrderById fail : query null");
            return null;
        }
        orders.stream().forEach(order -> this.fillOrderOtherInfos(order, orderItems));
        return orders;
    }

    @Override
    public PageHelper<UmsRentalOrder> queryOrdersForSite(PageHelper<UmsRentalOrder> pageHelper, QueryOrderCriteria queryCriteria, OrderItem... orderItems) {
        log.debug("queryOrdersForSite and pageHelper:{} \r\n queryCriteria:{} \r\n orderItems:{}", pageHelper, queryCriteria, orderItems);
        // 获得查询参数
        Map<String, Object> params = queryCriteria.getQueryMapForSite();

        // 查询出来的订单
        List<UmsRentalOrder> orders = umsRentalOrderMapper.queryOrdersForSite(pageHelper.getQueryParams(params, umsRentalOrderMapper.queryOrderCountForSite(params)));

        // 设置订单的其他信息
        if (!CollectionUtils.isEmpty(orders)) {
            orders.stream().forEach(order -> this.fillOrderOtherInfos(order, ArrayUtils.add(orderItems, OrderItem.BACK_PROGRESS)));
        }

        return pageHelper.setListDates(orders);
    }


    @Override
    public int updateOrderPayed(String orderCode, long customerId, int isPredepositPay, String transCode, String channel) {
        return 0;
    }

    @Override
    public UmsRentalOrder queryOrderDetailById(long orderId, long customerId) {
        HashMap<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("order_id",orderId);
        paramsMap.put("user_id",customerId);
        return umsRentalOrderMapper.queryOrderDetailById(paramsMap);
    }

    @Override
    public int cancelOrder(long id, long storeId, String reason) {
        log.debug("cancelOrder and id:{} \r\n storeid:{}", id, storeId);
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("reason", reason);
        return umsRentalOrderMapper.cancelOrder(params);
    }

    @Override
    public UmsRentalOrder queryToPayOrder(String orderCode, long customerId, OrderItem... orderItems) {
        log.debug("queryToPayOrder and orderCode:{} \r\n customerId:{}", orderCode, customerId);

        Map<String, Object> params = new HashMap<>();
        params.put("orderCode", orderCode);
        params.put("customerId", customerId);

        return umsRentalOrderMapper.queryToPayOrder(params);
    }


    private void fillOrderOtherInfos(UmsRentalOrder order, OrderItem... orderItems) {

    }
}
